#coding=utf-8
import os, urllib
from BeautifulSoup import BeautifulSoup
import httplib2, urllib2, time

urlopen = urllib.urlopen

markets_for_short = {'上海':'sh', '深圳':'sz'}

def get_soup(url, encoding='gb18030'):
    content = urlopen(url).read()
    htmltext = content.decode(encoding, 'ignore').encode('u8')
    s = BeautifulSoup(htmltext, fromEncoding='u8')
    return s

homedir = os.path.expanduser('~')
def get_soup2(url, is_cached=False, encoding='gb18030', try_times=10, timeout=15):
    if is_cached:
        h = httplib2.Http("%s/stcache"%homedir, timeout=timeout)
    else:
    
        h = httplib2.Http(timeout=timeout)
        
    for i in range(try_times):
        try:
            resp, content = h.request(url)
        except Exception, e:
            print e
        else:
            if resp.status != 200:
                print 'status:',resp.status
                continue
            
            if resp['content-type'] and not resp['content-type'].startswith('text'): 
                print 'content-type:',resp['content-type']
                continue
            
            htmltext = content.decode(encoding, 'ignore').encode('u8')
            s = BeautifulSoup(htmltext, fromEncoding='u8')
            return s
            
def get_jd(month):
    return (month-1)/3 + 1
    
def get_yjs(sy, sjd, ey, ejd):
    '''
    jd 代表季度
    eg: get_yjs(2008, 2, 2009, 1) ==>
        [['2008', '2'], ['2008', '3'], ['2008', '4'], ['2009', '1']]
        
        注意:all params are integer
    '''
    l = []
    if sy == ey:
        for jd in range(sjd, ejd+1):
            l.append([sy, jd])
        return l
        
    for jd in range(sjd, 5):
        l.append( [sy, jd] )
        
    years = range(sy+1, ey)    
    for y in years:
        for jd in range(1, 5):
            l.append( [y, jd] )
    
    for jd in range(1, ejd+1):
        l.append( [ey, jd] )
    
    l = [[str(i[0]), str(i[1])] for i in l]
    return l
